/* 满30 -6
ITEM0001 ITEM0022 ITEM0013 半价*/
//arr 订单
function bestcharge(arr) {
  var itemss = require("./items");

  var items = itemss.getItems(); //全部菜品名单
  // console.log(items)
  var orderitems = readItems(arr); //点菜的名单
  // console.log(orderitems);
  var oldmoney = getOldPrice(orderitems, items); //原价
  // console.log(oldmoney);
  var price1 = getPrice1(oldmoney);
//   console.log(price1);
  var price2Obj = getPrice2(orderitems, items);
//   console.log(price2Obj);
  check(price1, price2Obj, oldmoney);
// console.log(orderitems);

  
  function readItems(arr) {
    //返回菜品与数目
    var orderitems = [];
    for (let i = 0; i < arr.length; i++) {
      var temp = {};
      var id = arr[i].split("x")[0];
      var num = arr[i].split("x")[1];
      temp.id = id;
      temp.num = num;
      orderitems.push(temp);
    }

    return orderitems;
  }

  function getOldPrice(orderitems, items) {
    //获得原价
    var tempid = [];
    var temprice = [];
    items.forEach((element) => {
      tempid.push(element.id);
      temprice.push(element.price);
    });

    // console.log(tempid);
    // console.log(temprice);;

    var oldmoney = orderitems.reduce(function (total, item, array) {
      //   console.log(item);

      var id = tempid.indexOf(item.id);
      // var id = tempid.indexOf('ITEM0013')
      // console.log(id);

      return total + item.num * temprice[id];
    }, 0);
    return oldmoney;
  }

  function getPrice1(oldmoney) {
    //方案一价格 tag 0 使用会员 tag 1 不使用会员
    return oldmoney >= 30
      ? { price1: oldmoney - 6, tag: 1 }
      : { price1: oldmoney, tag: 0 };
  }

  function getPrice2(orderitems, items) {
    var tempid = [];
    var temprice = [];
    items.forEach((element) => {
      tempid.push(element.id);
      temprice.push(element.price);
    });

    // console.log(tempid);
    // console.log(temprice);;

    var savemoney = 0;
    var oldmoney = orderitems.reduce(function (total, item, array) {
      // console.log(item);

      var id = tempid.indexOf(item.id);
      if (id === 0 || id === 2) {
        total += item.num * temprice[id] * 0.5;
        savemoney += item.num * temprice[id] * 0.5;
      }

      if (id === 1) total += item.num * temprice[id];

      return total;
    }, 0);

    return { price2: oldmoney, save: savemoney };
  }

  function order(orderitems){
    //   订单详情
    console.log(`============= 订餐明细 =============`);
    
    var tempid = [];
    var tempname = [];
    var temprice= []
    items.forEach((element) => {
      tempid.push(element.id);
      tempname.push(element.name);
      temprice.push(element.price);
    });

    // console.log(tempid);
    // console.log(temprice);;

    orderitems.reduce(function (total, item, array) {
      //   console.log(item);

      var id = tempid.indexOf(item.id);
      // var id = tempid.indexOf('ITEM0013')
      // console.log(id);

      console.log(`${tempname[id]}  X  ${item.num} = ${temprice[id]*item.num}元`);
      
    }, 0);
    
  }
  function check(price1, price2Obj, oldmoney) {
    console.log(price2Obj.savemoney);

    if (price2Obj.save) {
      //   console.log(111);

      if (price1.price1 <= price2Obj.price2){
order(orderitems)
     
        console.log(`-----------------------------------
使用优惠:
满30减6元，省6元
-----------------------------------
总计:${price1.price1}元
===================================`);
    }
      else{     
order(orderitems)
console.log(`
-----------------------------------
使用优惠:
指定菜品半价(黄焖鸡，凉皮)，省 ${price2Obj.save} 元
-----------------------------------
总计：${price2Obj.price2} 元
===================================
`);


        }; 
    }else if(price1.price1<oldmoney){     
        order(orderitems)
        console.log(`
        -----------------------------------
        使用优惠:
        指定菜品半价(黄焖鸡，凉皮)，省 ${price2Obj.save} 元
        -----------------------------------
        总计：${price2Obj.price2} 元
        ===================================
        `);
        
        
                }
    else {order(orderitems)
        console.log(`原价${oldmoney}元
==================================`);
        }
       
    
  }
}

// bestcharge(["ITEM0013 x 4", "ITEM0022 x 1"]);
bestcharge(["ITEM0013 x 4"]);
